package org.qq.serverthread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import org.qq.util.ServerAllData;

/**
 * 
 * project_name:QQ
 * package_name:org.qq.serverthread
 * file_name:ServerDiposeThread.java
 * @author Link
 * create_date:2015年9月1日 上午9:38:14
 * modified_date:
 * description: 服务器监听客户端发消息过来线程根据收到的消息进行相应处理
 *
 */
public class ServerDiposeThread extends Thread{
	private Socket s;							//套接字
	private BufferedReader br;		//缓冲字符读取流
	private String str;
	private PrintStream ps;						//向客户端发送消息输出流
	
	public ServerDiposeThread() {
		super();
	}

	public ServerDiposeThread(Socket s) {
		super();
		this.s = s;
		try {
			br = new BufferedReader(new InputStreamReader(s.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

	@Override
	public void run() {
		while(true){
			try {
				str = br.readLine();
			} catch (IOException e) {
				return;//扑捉到异常直接跳出循环
			}finally{
			}
			if(str.endsWith("regist")){//注册
				new SerRegistReply(str,s).sendMessageToResClient();//注册
			}else if(str.endsWith("login")){//登录
				new SerLoginReply(str,s).sendMessagetToLonClient();//登录
			}else if(str.endsWith("modify")){//修改
				new SerModifyReply().sendMessageToModClient(str, s);//修改用户信息
			}else if(str.endsWith("groupChat")){//群聊					//群聊
				sendMessageToGroupChat(str);						//群聊方法
			}else if(str.endsWith("----close")){//下线
				userOfflineInformation(s, str.substring(0, 8));//发送用户下线消息至客户端
				delateOnlineUser(str);    //调用移除用户方法
			}else if(str.endsWith("privateChat")){//私聊
				sendMssageToPrivateChat(str);//调用私聊方法
			}
		}
	}
	
	/**
	 * 
	 * description: 发群消息到客户端
	 * 2015年9月1日 下午3:33:06
	 * @author Link
	 * void
	 *
	 */
	public synchronized void sendMessageToGroupChat(String str){
		String str1 = ServerAllData.mapList.get(s);//获取对应的用户昵称信息
		for(int i = 0;i<ServerAllData.socket.size();i++){		//遍历socket集合
			try {
				ps = new PrintStream(ServerAllData.socket.get(i).getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			ps.println(str1+"::::"+str);
		}
	}
	
	/**
	 * 
	 * description: 私人聊天
	 * creatTime:2015年9月5日上午11:28:56
	 * @author Link
	 * return type: void
	 *
	 */
	public synchronized void sendMssageToPrivateChat(String str){
		String message = str.substring(0, str.length()-23);//发送的消息
		String name = str.substring(message.length(), message.length()+8);//截取发消息到指定的账号对象
		String oldName = ServerAllData.mapList.get(s)+"("+ServerAllData.nameList.get(s)+")";//发消息者
		Socket s1 = ServerAllData.nameToSocket.get(name);//获得对应的Socket
		PrintStream ps1 = null;
		if(s1 == null){
			return;
		}else{
			try {
				ps1 = new PrintStream(s1.getOutputStream());
				ps1.println(message+"--%%"+oldName+"----privateChat");//将消息发至指定好友处
			} catch (IOException e) {
				e.printStackTrace();
			}
		}	
	}
	
	/**
	 * 
	 * description: 移除下线用户
	 * creatTime:2015年9月3日上午12:34:25
	 * @author Link
	 * return type: void
	 *
	 */
	public synchronized void delateOnlineUser(String str){
		ServerAllData.onlineName.remove(str.substring(0, 8));//在ServerAllData中移除该用户
		ServerAllData.socket.remove(s);//移调Socket
		ServerAllData.mapList.remove(s);
		ServerAllData.nameList.remove(s);
		ServerAllData.nameToNickName.remove(str.substring(0, 8));
		ServerAllData.nameToSocket.remove(str.substring(0, 8));
	}
	
	/**
	 * 
	 * description: 发送好友下线消息至客户端
	 * creatTime:2015年9月5日上午12:46:57
	 * @author Link
	 * return type: void
	 *
	 */
	public synchronized void userOfflineInformation(Socket s,String namestr){
		PrintStream ps1 = null;
		for(int i = 0;i<ServerAllData.socket.size();i++){
			if(s!=ServerAllData.socket.get(i)){
				try {
					ps1 = new PrintStream(ServerAllData.socket.get(i).getOutputStream());
					String nickName = ServerAllData.nameToNickName.get(namestr);
					ps1.println("----offline----"+nickName+"("+namestr+")");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
}




